package com.team.kaidb.customview;

import android.app.Activity;
import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.team.kaidb.R;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.InjectViews;
import butterknife.OnClick;

/**
 * Created by Wj on 15/7/1.
 * 小键盘自定义
 */
public class KeyBoardView {

    Context context;
    Activity activity;

    //右边的三列
    EditText et_value;
    TextView tv_number_0;
    TextView tv_number_1;
    TextView tv_number_2;
    TextView tv_number_3;
    TextView tv_number_4;
    TextView tv_number_5;
    TextView tv_number_6;
    TextView tv_number_7;
    TextView tv_number_8;
    TextView tv_number_9;
    TextView tv_number_dot;
    TextView tv_number_del;
    //左边的一列
    TextImage tv_number_col1;
    ImageView tv_number_col2;
    ImageView tv_number_col3;
    TextView tv_number_col4_1;
    ImageView tv_number_col4_2;


    public KeyBoardView(Activity activity) {
        this.activity = activity;
        context = activity;
        et_value = (EditText) activity.findViewById(R.id.editText_value);
        et_value.setInputType(InputType.TYPE_NULL);
        tv_number_0 = (TextView) activity.findViewById(R.id.textView_number_0);
        tv_number_1 = (TextView) activity.findViewById(R.id.textView_number_1);
        tv_number_2 = (TextView) activity.findViewById(R.id.textView_number_2);
        tv_number_3 = (TextView) activity.findViewById(R.id.textView_number_3);
        tv_number_4 = (TextView) activity.findViewById(R.id.textView_number_4);
        tv_number_5 = (TextView) activity.findViewById(R.id.textView_number_5);
        tv_number_6 = (TextView) activity.findViewById(R.id.textView_number_6);
        tv_number_7 = (TextView) activity.findViewById(R.id.textView_number_7);
        tv_number_8 = (TextView) activity.findViewById(R.id.textView_number_8);
        tv_number_9 = (TextView) activity.findViewById(R.id.textView_number_9);
        tv_number_dot = (TextView) activity.findViewById(R.id.textView_number_dot);
        tv_number_del = (TextView) activity.findViewById(R.id.textView_number_del);
        /////////
        tv_number_col1 = (TextImage) activity.findViewById(R.id.textView_col01);
        tv_number_col2 = (ImageView) activity.findViewById(R.id.textView_col02);
        tv_number_col3 = (ImageView) activity.findViewById(R.id.textView_col03);
        tv_number_col4_1 = (TextView) activity.findViewById(R.id.textView_col04_1);
        tv_number_col4_2 = (ImageView) activity.findViewById(R.id.textView_col04_2);

//        initViews();
    }

    /**
     * 设置监听和tag  同时设置左侧一栏的实际显示  1是图片，2是文字
     */
    public void initViews(int style) {
        tv_number_col1.setContent("微信支付", R.drawable.icon_wx);
        if (style == 1){
            tv_number_col4_1.setVisibility(View.GONE);
            tv_number_col4_2.setVisibility(View.VISIBLE);
        }else{
            tv_number_col4_1.setVisibility(View.VISIBLE);
            tv_number_col4_2.setVisibility(View.GONE);
        }
        /////////
        tv_number_0.setOnClickListener(new onNumSelectListener());
        tv_number_1.setOnClickListener(new onNumSelectListener());
        tv_number_2.setOnClickListener(new onNumSelectListener());
        tv_number_3.setOnClickListener(new onNumSelectListener());
        tv_number_4.setOnClickListener(new onNumSelectListener());
        tv_number_5.setOnClickListener(new onNumSelectListener());
        tv_number_6.setOnClickListener(new onNumSelectListener());
        tv_number_7.setOnClickListener(new onNumSelectListener());
        tv_number_8.setOnClickListener(new onNumSelectListener());
        tv_number_9.setOnClickListener(new onNumSelectListener());
        tv_number_dot.setOnClickListener(new onNumSelectListener());
        tv_number_del.setOnClickListener(new onNumSelectListener());
        //////
        tv_number_col1.setOnClickListener(new onNumSelectListener());
        tv_number_col2.setOnClickListener(new onNumSelectListener());
        tv_number_col3.setOnClickListener(new onNumSelectListener());
        tv_number_col4_1.setOnClickListener(new onNumSelectListener());
        tv_number_col4_2.setOnClickListener(new onNumSelectListener());
        //////
        tv_number_0.setTag(NumKeys.Num0);
        tv_number_1.setTag(NumKeys.Num1);
        tv_number_2.setTag(NumKeys.Num2);
        tv_number_3.setTag(NumKeys.Num3);
        tv_number_4.setTag(NumKeys.Num4);
        tv_number_5.setTag(NumKeys.Num5);
        tv_number_6.setTag(NumKeys.Num6);
        tv_number_7.setTag(NumKeys.Num7);
        tv_number_8.setTag(NumKeys.Num8);
        tv_number_9.setTag(NumKeys.Num9);
        tv_number_dot.setTag(NumKeys.Numdot);
    }

    private StringBuilder resValue = new StringBuilder();

    /**
     * 数字键监听
     */
    class onNumSelectListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.textView_col01:
                    Toast.makeText(context,"微信支付",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.textView_col02:
                    Toast.makeText(context,"支付宝支付",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.textView_col03:
                    Toast.makeText(context,"银联支付",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.textView_col04_1:
//                    Toast.makeText(context,"返回",Toast.LENGTH_SHORT).show();
                    activity.finish();
                    break;

                case R.id.textView_col04_2:
                    Toast.makeText(context,"VIP支付",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.textView_number_del:
                    if (resValue.length() > 0){
                        resValue.deleteCharAt(resValue.length()-1);
                    }
                    break;
                case R.id.textView_number_dot:
                    if (resValue.length() <= 0){
                        resValue.append("0").append(v.getTag());
                        return;
                    }
                    if(resValue.indexOf(v.getTag().toString()) == -1){
                        resValue.append(v.getTag());
                        return;
                    }
                    break;
                default:
                    resValue.append(v.getTag());
                    break;
            }
            et_value.setText(resValue.toString());
        }
    }

    /** 获取输入值
     */
    public float getResValue(){
        return Float.parseFloat(resValue.toString());
    }



}
